package com.awspaas.user.apps.processforsap.event;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.TaskInstance;
import com.actionsoft.bpms.bpmn.engine.servicetask.ServiceDelegate;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.processforsap.model.MessageHeader;
import com.awspaas.user.apps.processforsap.util.Logs;
import com.awspaas.user.apps.processforsap.util.ParentInterface;

import java.lang.reflect.Constructor;
import java.util.UUID;

public class UniversalBPMToSAPTask extends ServiceDelegate {


    @Override
    public boolean execute(ProcessExecutionContext pre){
        ProcessInstance processInstance = pre.getProcessInstance();
        String bindId = processInstance.getId();

        TaskInstance taskInstance = pre.getTaskInstance();
        String interfaceId = SDK.getRepositoryAPI().getActivityExtendAttribute(processInstance.getProcessDefId(),taskInstance.getActivityDefId() , "INTERFACE_NO");
        String interfaceName = "";
        String logId = "";
        String resultSendData = "";
        String returnData = "";
        ParentInterface parentInterface = null;
        String url = "";
        try {
            //查询接口维护配置表
            BO bo = SDK.getBOAPI().query("BO_EU_SERVICE_INTERFACE_CONFIG").addQuery("INTERFACE_NO=", interfaceId).detail();
            if (bo == null) {
                throw new BPMNError("0331","未找到配置表数据，请核查BPM配置！");
            } else {
                /**
                 * 业务代码
                 */
                String code = bo.getString("CODE");
                /**
                 * 接口名称
                 */
                logId = UUID.randomUUID().toString();
                interfaceName = bo.getString("INTERFACE_NAME");
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("BINDID", bindId);
                jsonObject.put("LOGID", logId);
                JSONObject messageHeader = new JSONObject();
                messageHeader.put("Interface_ID", interfaceId);
                messageHeader.put("UUID", bindId);
                messageHeader.put("MessageId", "");
                messageHeader.put("DisId", "");
                jsonObject.put("MessageHeader", messageHeader);
                Class<?> clzz = Class.forName(code);
                Constructor<?> con = clzz.getConstructor(JSONObject.class);
                parentInterface  = (ParentInterface)con.newInstance(jsonObject);
                parentInterface.dueInterFaceData();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BPMNError("0331",e.getMessage());
        } finally {
            resultSendData = parentInterface.resultSendData;
            returnData = parentInterface.returnData;
            parentInterface.resultType = false;
            try{
                JSONObject returnJSON = JSONObject.parseObject(returnData);
                JSONObject responses=returnJSON.getJSONObject("Responses");
                parentInterface.resultType = "S".equals(responses.getString("Type"))?true:false;
//                returnData = responses.getString("MESSAGE");
            }catch (Exception e){
                e.printStackTrace();
//                returnData = e.getMessage();
                parentInterface.resultType = false;
            }
            Logs.addLog(interfaceName, interfaceId, "send", "BPM", "SAP", resultSendData, returnData,parentInterface.resultType, "", "1", bindId,"",logId);
            if (parentInterface.resultType){
                return true;
            }else{
                throw new BPMNError("0331",returnData);
            }
        }
    }
}